var express = require('express');
//npm install cookie-parser --save
var cookieParser = require('cookie-parser'); //引用中间件

var router = express.Router();
var app = express();

app.use(cookieParser());

//写入cookie
router.get('/set', function (req, res) {
    //httpOnly:true表示只能在nodejs服务端可以操作cookie,不能使用js脚本操作,换其他后端语言比如php,java也是一样的原理
    //有效期为60000ms,即60秒以后到期
    res.cookie("username",'hello',{maxAge:60000,path:'/',domain:'localhost',httpOnly:true}); //设置cookie和过期时间
    res.send("set cookie sucessful");
});
//获取cookie
router.get('/get', function (req, res) {
    res.send(req.cookies.username);
});
//删除cookie
router.get('/del', function (req, res) {
    res.clearCookie("username");
    res.send('delete success!');
});

module.exports = router;
